package pl.umk.mat.fred.unicore.urc.storagemanagement.actions;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.Action;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;

import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.BelowStorageNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.CatalogNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.FileNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.MultiplyCatalogNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.Node;
import pl.umk.mat.fred.unicore.urc.storagemanagement.tree.nodes.StorageNode;
import pl.umk.mat.fred.unicore.urc.storagemanagement.unicore.Storage;
import pl.umk.mat.fred.unicore.urc.storagemanagement.views.StorageBrowserView;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;

import de.fzj.unicore.wsrflite.xmlbeans.BaseFault;

public class RenameAction extends Action {
	private StorageBrowserView view;
	
	public RenameAction(StorageBrowserView view) {
		super();
		this.view = view;
		this.setText("Rename");
	}
	
	public void run() {
		ISelection selection = view.getTreeViewer().getSelection();
		Object obj = ((IStructuredSelection)selection).getFirstElement();
		BelowStorageNode nn = (BelowStorageNode) obj;
		
		if (obj instanceof MultiplyCatalogNode) {
			if (!MessageDialog.openConfirm(PlatformUI.getWorkbench().getDisplay().getActiveShell(),
					"Confirm", "This catalog is linked with many other catalogs, are you sure you want to rename them?")) {
				return;
			}
		}
		
		InputDialog dialog = new InputDialog(PlatformUI.getWorkbench().getDisplay().getActiveShell(),
				"Rename", "Enter new name", nn.getRealName(), null);
		dialog.open();
		if (dialog.getReturnCode() == 0) {
			final Map<BelowStorageNode, String> nodesToRename = new HashMap<BelowStorageNode, String>();
			if (obj instanceof MultiplyCatalogNode) {
				MultiplyCatalogNode mNode = (MultiplyCatalogNode) obj;
				for (CatalogNode cn : mNode.getCatalogs()) {
					String newPath = cn.getPath().substring(0, cn.getPath().lastIndexOf("/") + 1) + dialog.getValue();
					nodesToRename.put(cn, newPath);
				}
			} else 	if (obj instanceof BelowStorageNode) {
				BelowStorageNode n = (BelowStorageNode) obj;
				String newPath = n.getPath().substring(0, n.getPath().lastIndexOf("/") + 1) + dialog.getValue();
				nodesToRename.put(n, newPath);
			} 
			if (!nodesToRename.isEmpty()) {
				Job job = new Job("Rename file") {
					@Override
					protected IStatus run(IProgressMonitor monitor) {
						monitor.beginTask("renaming", nodesToRename.size() > 1 ? nodesToRename.size() : IProgressMonitor.UNKNOWN);
						try{
							for (Iterator<Entry<BelowStorageNode, String>> itr = nodesToRename.entrySet().iterator(); itr.hasNext(); ) {
								if (monitor.isCanceled()) {
									return Status.CANCEL_STATUS;
								}
								Entry<BelowStorageNode, String> entry = itr.next();
								final BelowStorageNode node = entry.getKey();
								String newPath = entry.getValue(); 
								try {
									node.getStorage().rename(node.getPath(), newPath);
									node.setPath(newPath);
									view.getDoubleClickAction().run(node.getParent());
								} catch (BaseFault e) {
									e.printStackTrace();
								} finally {
									monitor.worked(1);
								}
							}
						} finally {
							monitor.done();
						}
						return Status.OK_STATUS;
					}
				};
				job.schedule();
			}
		}
	}
}
